package com.hzpd.hainan.module.news.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.hzpd.hainan.R;
import com.hzpd.hainan.app.ListBaseAdapter;
import com.hzpd.hainan.model.news.CommentBean;
import com.hzpd.hainan.utils.LogUtils;
import com.hzpd.hainan.utils.SPUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CommentAdapter extends ListBaseAdapter<CommentBean> {

	private SPUtil spu;
	private int which; // 用于标识 是1 我的评论中的adapter 还是 0新闻详情中的adapter
	private OnlicckChildCommentListener commentListener;
	public interface  OnlicckChildCommentListener{
		public void clickComment(CommentBean bean);
	}
	public static enum Itemtype {
		ONE(0), TWO(1);
		private int type;

		private Itemtype(int type) {
			this.type = type;
		}

		public int getType() {
			return type;
		}
	}

	public CommentAdapter(Activity context, int which) {
		super(context);
		spu = SPUtil.getInstance();
		this.which = which;
	}

	public CommentAdapter(Activity context, List<CommentBean> list) {
		super(context);
		this.list = list;
		spu = SPUtil.getInstance();
	}

	@Override
	public int getViewTypeCount() {
		return Itemtype.values().length;
	}

	@Override
	public int getItemViewType(int position) {
		CommentBean cb = list.get(position);
		if ("2".equals(cb.getCommenttype())) {
			return Itemtype.TWO.getType();
		} else {
			return Itemtype.ONE.getType();
		}
	}

	@Override
	public View getMyView(int position, View convertView, ViewGroup parent) {
		int type = getItemViewType(position);
		LogUtils.i("comment type-->" + type);
		ONE one = null;
		TWO two = null;
		THREE three = null;
		final CommentBean cb = list.get(position);
		final CommentBean replyto = cb.getReplyto();
		if (null == convertView) {
			switch (type) {
			case 0:
				convertView = inflater.inflate(R.layout.newscomm_list_item,
						parent, false);
				one = new ONE(convertView);
				convertView.setTag(one);
				break;
			case 1:
				if (which == 0) {
					convertView = inflater.inflate(
							R.layout.newscomm_list_item3, parent, false);
					three = new THREE(convertView);
					convertView.setTag(three);
				} else {
					convertView = inflater.inflate(
							R.layout.newscomm_list_item2, parent, false);
					two = new TWO(convertView);
					convertView.setTag(two);
				}

				break;

			}
		} else {
			switch (type) {
			case 0:
				one = (ONE) convertView.getTag();
				break;
			case 1:
				if (which == 0) {
					three = (THREE) convertView.getTag();
				} else {
					two = (TWO) convertView.getTag();
				}
				break;
			}
		}

		switch (type) {
		case 0:
			Glide.with(context).load(cb.getAvatar_path()).crossFade()
					.placeholder(R.drawable.default_bg).into(one.avatar);
			one.nickname.setText(cb.getNickname());
			one.time.setText(cb.getDateline());
			one.content.setText(cb.getContent());
			one.prasie_count.setText(cb.getPraise());
			one.reply_count.setText(cb.getCount());
			one.news_comment_prasie.setTag(position);
			one.news_comment_prasie.setOnClickListener(new PrasieAction());
			break;
		case 1:
			if (null != replyto) {
				if (which == 0) {
					Glide.with(context).load(cb.getAvatar_path()).crossFade()
							.placeholder(R.drawable.default_bg).into(three.avatar);
					three.nickname.setText(cb.getNickname());
					three.time.setText(cb.getDateline());
					three.content.setText(cb.getContent());
					three.prasie_count.setText(cb.getPraise());
					three.reply_count.setText(cb.getCount());
					three.replyto_nickname.setText(replyto.getNickname());
					three.reply_time.setText(replyto.getDateline());
					three.reply_content.setText(replyto.getContent());
					three.news_comment_prasie.setTag(position);
					three.news_comment_prasie
							.setOnClickListener(new PrasieAction());

					three.reply_content.setOnClickListener(new OnClickListener() {

								@Override
								public void onClick(View v) {
									if (commentListener != null){
										commentListener.clickComment(replyto);
									}
								}
							});
				} else {
					Glide.with(context).load(cb.getAvatar_path()).crossFade()
							.placeholder(R.drawable.default_bg).into(two.avatar);
					two.nickname.setText(cb.getNickname());
					two.time.setText(cb.getDateline());
					two.content.setText(cb.getContent());
					two.prasie_count.setText(cb.getPraise());
					two.reply_count.setText(cb.getCount());
					Glide.with(context).load(replyto.getAvatar_path()).crossFade()
							.placeholder(R.drawable.default_bg).into(two.reply_avatar);
					two.replyto_nickname.setText(replyto.getNickname());
					two.reply_time.setText(replyto.getDateline());
					two.reply_content.setText(replyto.getContent());
					two.reply_prasie.setText(replyto.getPraise());
					two.reply_comm.setText(replyto.getCount());
					two.news_comment_prasie.setTag(position);
					two.news_comment_prasie.setOnClickListener(new PrasieAction());
				}
			}
			break;
		}

		return convertView;
	}

	// 没有回复
	static class ONE {
		@BindView(R.id.newscomment_avatar)
		ImageView avatar;
		@BindView(R.id.newscomment_title)
		TextView nickname;
		@BindView(R.id.newscomment_time)
		TextView time;
		@BindView(R.id.newscomment_content)
		TextView content;
		@BindView(R.id.reply_count)
		TextView reply_count;
		@BindView(R.id.prasie_count)
		TextView prasie_count;
		@BindView(R.id.news_comment_prasie)
		LinearLayout news_comment_prasie;

		public ONE(View v) {
			ButterKnife.bind(this,v);

		}
	}
	// 有回复
	 static class TWO {
		@BindView(R.id.newscomment_avatar)
		 ImageView avatar;
		@BindView(R.id.newscomment_title)
		 TextView nickname;
		@BindView(R.id.newscomment_time)
		 TextView time;
		@BindView(R.id.newscomment_content)
		 TextView content;
		@BindView(R.id.reply_count)
		 TextView reply_count;
		@BindView(R.id.prasie_count)
		 TextView prasie_count;
		@BindView(R.id.reply_avatar)
		 ImageView reply_avatar;
		@BindView(R.id.replyto_nickname)
		 TextView replyto_nickname;
		@BindView(R.id.reply_time)
		 TextView reply_time;
		@BindView(R.id.reply_comm)
		 TextView reply_comm;
		@BindView(R.id.reply_prasie)
		 TextView reply_prasie;
		@BindView(R.id.reply_content)
		 TextView reply_content;
		@BindView(R.id.news_comment_prasie)
		 LinearLayout news_comment_prasie;
		@BindView(R.id.comment_replyto_ll)
		 LinearLayout comment_replyto_ll;
		@BindView(R.id.replyto_rl)
		 RelativeLayout replyto_rl;

		public TWO(View v) {
			ButterKnife.bind(this,v);

		}
	}

	// 有回复 样式2
	 static class THREE {
		@BindView(R.id.newscomment_avatar)
		 ImageView avatar;
		@BindView(R.id.newscomment_title)
		 TextView nickname;
		@BindView(R.id.newscomment_time)
		 TextView time;
		@BindView(R.id.newscomment_content)
		 TextView content;
		@BindView(R.id.reply_count)
		 TextView reply_count;
		@BindView(R.id.prasie_count)
		 TextView prasie_count;
		@BindView(R.id.replyto_nickname)
		 TextView replyto_nickname;
		@BindView(R.id.reply_time)
		 TextView reply_time;
		@BindView(R.id.reply_content)
		 TextView reply_content;
		@BindView(R.id.news_comment_prasie)
		 LinearLayout news_comment_prasie;
		@BindView(R.id.comment_replyto_ll)
		 LinearLayout comment_replyto_ll;
		@BindView(R.id.replyto_rl)
		 RelativeLayout replyto_rl;

		public THREE(View v) {
			ButterKnife.bind(this,v);

		}
	}

	class PrasieAction implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (null == spu.getUser()) {
				return;
			}
//			prasieToComm(v);
		}
	}

	public void setChildCommentListener(OnlicckChildCommentListener listener){
		commentListener = listener;
	}

}
